package market

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func Market(r *gin.Engine) {
	market := r.Group("/market")
	{
		customer(market)
		city(market)
		market.POST(sql.RequestAdd, marketAdd)
		market.POST(sql.RequestUpdate, marketUpdate)
		market.GET(sql.RequestData, marketData)
		market.GET(sql.RequestList, marketList)
		market.GET(sql.RequestDelete, marketDelete)
	}
}

func marketAdd(c *gin.Context) {
	sql.HttpAdd(c, &model.Market{}, nil)
}

func marketUpdate(c *gin.Context) {
	sql.HttpUpdates(c, &model.Market{}, nil)
}

func marketData(c *gin.Context) {
	sql.HttpDataById(c, &model.Market{})
}

func marketDelete(c *gin.Context) {
	sql.HttpDeleteById(c, &model.Market{})
}

func marketList(c *gin.Context) {
	sql.HttpList(c, &[]model.Market{}, func(db *gorm.DB) *gorm.DB {
		search := c.Query("search")
		if search != "" {
			db = db.Where("CONCAT(city, area, market) LIKE ? ", "%"+search+"%")
		}

		cityName := c.Query("city")
		if cityName != "" {
			db = db.Where("city = ? AND market != ?", cityName, "其他")
		}
		return db.Order("id DESC")
	})
}
